﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM

using System.Collections.Generic;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.ViewModel.OrderFlowManage;

#endregion

namespace YunSpace.Repository.OrderFlowManage
{
    public class ClientOrdertakelookDetailRepository : RepositoryBase<ClientOrdertakelookDetailEntity>, IClientOrdertakelookDetailRepository
    {
        public List<ClientOrdertakelookDetailDto> GetClientOrdertakelookDetailDtos(Pagination pagination, string orderId,string F_CheckUserId,string F_No)
        {
            var sql = $@"
                    SELECT distinct d.F_OrderId, d.F_SourceType, d.F_Source, 
                        l.F_Id AS F_OrderTakelookId, f.F_Reason, f.F_Remark, f.F_State, 
                        s.title AS F_SiteName, s2.title AS F_SpaceName
                    FROM Site_Client_OrderTakelook l
	               left join Site_Client_OrderTakelookDetail d ON l.F_OrderId = d.F_OrderId 
						  		AND l.F_SiteId = d.F_SiteId AND l.F_SpaceId = d.F_SpaceId
                    INNER JOIN Site_Client_Order AS o ON o.F_Id = l.F_OrderId
                    inner join Sys_CheckRecord c on l.F_OrderId=c.F_OrderId and l.F_No=c.F_No
                    LEFT JOIN SL_Site AS s ON s.F_Id = l.F_SiteId
                    LEFT JOIN SL_Space AS s2 ON s2.F_Id = l.F_SpaceId
                    left join Site_Client_OrderMapping f on l.F_Id=f.F_DetailId and f.F_CheckUserId=c.F_CheckUserId                   
                    WHERE l.F_OrderId = '{orderId}' and c.F_CheckUserId='{F_CheckUserId}' and c.F_No='{F_No}' and c.F_ApplyType=2";

            pagination.records = GetClientOrdertakelookDetailDtoCount(orderId, F_CheckUserId,F_No);

            var startNo = pagination.rows * (pagination.page - 1);
            var endNo = startNo + pagination.rows;

            sql = $"{sql} ORDER BY d.{pagination.sidx} LIMIT {startNo},{endNo};";

            using (var db = new RepositoryBase())
            {
                var result = db.FindList<ClientOrdertakelookDetailDto>(sql);
                return result;
            }
        }

        private int GetClientOrdertakelookDetailDtoCount(string orderId,string F_CheckUserId,string F_No)
        {
            var sql = $@"
                    SELECT COUNT(1)
                    FROM Site_Client_OrderTakelook AS l
                    INNER JOIN Site_Client_Order AS o ON o.F_Id = l.F_OrderId
                    inner join Sys_CheckRecord c on l.F_OrderId=c.F_OrderId
                    WHERE l.F_OrderId = '{orderId}' and c.F_CheckUserId='{F_CheckUserId}' and c.F_No='{F_No}'";

            return Count(sql);
        }
    }
}

